package com.example.smartcity_40.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.smartcity_40.R;

import java.util.ArrayList;
import java.util.List;


public class HelpFragment extends Fragment {

    private View view;
    private Context context;
    private ViewPager banner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_help, container, false);
        context = getContext();
        initview();
        return view;
    }

    private void initview() {
        banner = view.findViewById(R.id.banner);
        ArrayList<Integer> imglist = new ArrayList<>();
        imglist.add(R.drawable.fu_img02);
        imglist.add(R.drawable.fu_img4);
        imglist.add(R.drawable.fu_img5);
        BannerIMGAdapter bannerIMG = new BannerIMGAdapter(context,imglist);
        banner.setAdapter(bannerIMG);
        handler.sendEmptyMessageDelayed(0,3*1000);
    }

    Handler handler  = new Handler(){
        @Override
        public void dispatchMessage(@NonNull Message msg) {
            super.dispatchMessage(msg);
            if(msg.what==0){
                int currentItem = banner.getCurrentItem();
                currentItem = (currentItem+1)%3;
                banner.setCurrentItem(currentItem);
                handler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };

    class  BannerIMGAdapter extends PagerAdapter{

        private  Context context;
        private List<Integer> list ;

        public BannerIMGAdapter(Context context, List<Integer> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list==null?0:list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View)object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView imageView = null;
            try {
                imageView = new ImageView(context);
                imageView.setImageResource(list.get(position));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                container.addView(imageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return imageView;
        }
    }
}